package com.example.jDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author 郅占伟
 * @ClassName MyDialog
 * @Description TODO
 * @Date 2024年05月14日
 * @Version: 1.0
 */
public class MyDialog {
    public static void genDialog() {
        JButton jButton = new JButton("模式对话框");
        JFrame jFrame = new JFrame("模式对话框测试");

        jFrame.setSize(500, 450);
        jFrame.setLocation(300, 200);
        jFrame.setLayout(new FlowLayout());

        // 添加按钮
        jFrame.add(jButton);

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

        JLabel jLabel = new JLabel();
        JDialog jDialog = new JDialog(jFrame, "模式对话框");
        jDialog.setSize(220, 150);
        jDialog.setLocation(350, 250);
        jDialog.setLayout(new FlowLayout());

        JButton jButtonDialogConfirm = new JButton("确定");
        JButton jButtonDialogCancel = new JButton("取消");
        jDialog.add(jButtonDialogConfirm);
        jDialog.add(jButtonDialogCancel);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jDialog.setModal(true);

                if (jDialog.getComponents().length == 1) {
                    jDialog.add(jLabel);
                }

                jLabel.setText("模式对话框，点击确定按钮关闭");
                jDialog.setVisible(true);
            }
        });

        // 确定按钮
        jButtonDialogConfirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton source = (JButton) e.getSource();
                jDialog.setVisible(false);
            }
        });


    }
}
